package model.board;

import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BoardService {
	private BoardDao boardDao;
	public void setBoardDao(BoardDao boardDao){
		this.boardDao=boardDao;
	}
	
	/**
	 * 게시글 쓰기
	 * @param BoardVO : board_no, writer, title, content
	 */
	public void write(BoardVO bvo) throws SQLException{
		int sub_content_no=boardDao.getMaxSubContentNo(bvo.getBoard_no())+1; //게시판의 마지막 게시글 번호 받아와서 +1
		bvo.setSub_content_no(sub_content_no);
		int content_no=boardDao.write(bvo);
		String date=boardDao.selectByNoForDate(content_no);
		bvo.setWriteDate(date);
	}
	
	/**
	 * 게시글 목록 불러오기
	 * @param map : map.pageNo-페이지번호 map.boardNo-게시판번호
	 * @return ListVO : list-게시글 목록 paging-페이지 정보
	 */
	public ListVO getContentList(Map map) throws SQLException{
		String pageNo=(String) map.get("pageNo");
		String boardNo=(String) map.get("boardNo");
		String view=(String) map.get("view");
		if(pageNo==null||pageNo==""){ 
			pageNo="1";
			map.put("pageNo", pageNo);
		}
		int total=boardDao.totalContent(boardNo);
		PagingBean paging;
		if(view==null||view.equals("")){
			paging=new PagingBean(total,Integer.parseInt(pageNo));
			view="15";
			map.put("view", view);
		}else{
			paging=new PagingBean(total,Integer.parseInt(pageNo),Integer.parseInt(view));
		}
		String boardName=boardDao.getBoardName(boardNo);
		List<BoardVO> list=boardDao.getGuestList(map);
		ListVO lvo=new ListVO(boardName,list,paging);
		return lvo;
	}
	
	/**
	 * 게시글 보기
	 * @param content_no
	 * @return BoardVO
	 */
	public ShowVO showContent(String no) throws SQLException{
		boardDao.updateCount(no);
		BoardVO bvo=boardDao.showContent(no);
		bvo.setBoard_name(boardDao.getBoardName(bvo.getBoard_no()+""));
		List<ReflyVO> list=boardDao.reflyList(no);
		ShowVO svo= new ShowVO(bvo, list);
		return svo;
	}
	
	/**
	 * 게시글 삭제
	 * @param content_no
	 */
	public void deleteBoard(String no) throws SQLException{
		boardDao.deleteBoard(no);
	}
	
	/**
	 * 게시글 수정
	 * @param BoardVO
	 */
	public void updateBoard(BoardVO bvo) throws SQLException{
		boardDao.updateBoard(bvo);
	}

	/**
	 * 리플 쓰기
	 * @param BoardVO
	 */
	public void answerBoard(BoardVO bvo) throws SQLException {
		int sub_content_no=boardDao.getMaxSubContentNo(bvo.getBoard_no())+1; //게시판의 마지막 게시글 번호 받아와서 +1
		bvo.setSub_content_no(sub_content_no);
		boardDao.answerSort(bvo);
		int no=boardDao.answerBoard(bvo);
		String date=boardDao.selectByNoForDate(no);
		bvo.setWriteDate(date);
	}
	
	public void uploadFileBoard(BoardVO bvo)throws SQLException{
		boardDao.uploadFileBoard(bvo);
	}
	public void deleteFile(String newfilename) {
		File f=new File(newfilename);
		System.out.println(newfilename+" file remove:"+f.delete());
	}
	public void deleteFileBoard(String no) throws SQLException {
		boardDao.deleteFileBoard(no);
	}

	/**
	 * 미니 리플 쓰기
	 * @param bvo
	 * @throws SQLException
	 */
	public void writeRefly(ReflyVO rvo) throws SQLException {
		int no=boardDao.writeRefly(rvo);
		String date=boardDao.selectByNoForDate(no);
		rvo.setWrite_date(date);
	}

	/**
	 * 미니 리플 수정
	 * @param no
	 * @param content
	 * @throws SQLException
	 */
	public void modifyRefly(Map map) throws SQLException {
		boardDao.modifyRefly(map);
	}

	/**
	 * 미니 리플 삭제
	 * @param no
	 * @throws SQLException
	 */
	public void deleteRefly(String no) throws SQLException {
		boardDao.deleteRefly(no);
	}

	/**
	 * 게시글 검색
	 * @param map
	 * @return
	 */
	public ListVO searchBoard(HashMap map)  throws SQLException{
		String board_no=(String) map.get("board_no");
		String view=(String) map.get("view");
		String page_no=(String) map.get("page_no");
							System.out.println("service : "+map);
		int total=boardDao.totalSearchContent(map);
		PagingBean paging;
		if(page_no==null||page_no==""){ 
			page_no="1";
			map.put("page_no", page_no);
		}
		if(view==null||view.equals("")){
			paging=new PagingBean(total, Integer.parseInt(page_no), 15, true);
			view="15";
			map.put("view", view);
		}else{
			paging=new PagingBean(total, Integer.parseInt(page_no),Integer.parseInt(view), true);
		}
							System.out.println("service : "+map);
		String boardName=boardDao.getBoardName(board_no);
		List<BoardVO> list=boardDao.getSearchBoardList(map);
		ListVO lvo=new ListVO(boardName,list,paging);
		return lvo;
	}

	/**
	 * 모든 게시판의 게시글 통합목록 불러오기
	 * @param map : map.pageNo-페이지번호
	 * @return ListVO : list-게시글 목록 paging-페이지 정보
	 */
	public ListVO getContentListAll(HashMap map) throws SQLException{
							System.out.println("서비스 갓들어옴.");
		String view=(String) map.get("view");
		String page_no=(String) map.get("page_no");
							System.out.println("service : "+map);
		int total=boardDao.totalContent();
		PagingBean paging;
		if(page_no==null||page_no==""){ 
			page_no="1";
			map.put("page_no", page_no);
		}
		if(view==null||view.equals("")){
			paging=new PagingBean(total, Integer.parseInt(page_no), 15);
			view="15";
			map.put("view", view);
		}else{
			paging=new PagingBean(total, Integer.parseInt(page_no),Integer.parseInt(view));
		}
						System.out.println("토탈끝 고고"+total);
						System.out.println("service : "+map);
						System.out.println("service : "+paging.getNowPage());
		List<BoardVO> list=boardDao.getBoardListAll(map);
						System.out.println("서비스에서 보드리스트 끝나고나서 뜨나?"+list.size());
		ListVO lvo=new ListVO(null,list,paging);
		return lvo;
	}
	
	/**
	 * 모든 게시판의 게시글 검색 (방명록 제외)
	 * @param map
	 * @return
	 */
	public ListVO allSearchBoard(HashMap map)  throws SQLException{
		String view=(String) map.get("view");
		String page_no=(String) map.get("page_no");
			System.out.println("service : "+map);
		int total=boardDao.allTotalSearchContent(map);
		PagingBean paging;
		if(page_no==null||page_no==""){ 
			page_no="1";
			map.put("page_no", page_no);
		}
		if(view==null||view.equals("")){
			paging=new PagingBean(total, Integer.parseInt(page_no), 15, true);
			view="15";
			map.put("view", view);
		}else{
			paging=new PagingBean(total, Integer.parseInt(page_no),Integer.parseInt(view), true);
		}
						System.out.println("service : "+map);
						System.out.println("service : "+paging.toString());
						System.out.println("service : "+paging.getIsSearch());
		List<BoardVO> list=boardDao.getAllSearchBoardList(map);
		ListVO lvo=new ListVO(null,list,paging);
		return lvo;
	}

}







